/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmijcoltrane;

/**
 *
 * @author Felipe Gutierrez
 */


import java.util.ArrayList;
import java.util.Stack;

import net.sf.jColtrane.annotations.methods.BeforeElement;
import net.sf.jColtrane.annotations.methods.EndElement;
import net.sf.jColtrane.annotations.methods.StartElement;
import net.sf.jColtrane.handler.AttributesHolder;
import net.sf.jColtrane.handler.ContextVariables;
import net.sf.jColtrane.handler.SAXEvent;

import xmibase.XmiAttribute;
import xmibase.XmiClass;
import xmibase.XmiPackage;

public class XmiGetAttribute {

	public ArrayList<XmiClass> classes;
	public ArrayList<XmiAttribute> attributes;

	public XmiPackage rootPackage;

	public Stack elements;

	public XmiGetAttribute(XmiPackage packages, Stack element) {

		this.classes = new ArrayList<XmiClass>();
		this.attributes = new ArrayList<XmiAttribute>();

		this.elements = element;
		this.rootPackage = packages;
	}
	/**
	 * Start UML:Attributeo at UML:Class
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 2, tag = "UML:Class")
	@StartElement(tag = "UML:Attribute")
	public void startElementAttribute(ContextVariables contextVariables) {
		XmiAttribute xmiAtt = new XmiAttribute();

		SAXEvent event = contextVariables.getLastEvent();
		AttributesHolder attElements = event.getAtributesHolder();

		System.out.println("		Start Attribute: "
				+ attElements.getValue("name"));
		{
			xmiAtt.setId(attElements.getValue("xmi.id"));
			xmiAtt.setName(attElements.getValue("name"));
			xmiAtt.setVisibility(attElements.getValue("visibility"));
			xmiAtt.setOwnerScope(attElements.getValue("ownerScope"));
			xmiAtt.setSpecification(Boolean.parseBoolean(attElements
					.getValue("isSpecification")));
			xmiAtt.setChangeability(attElements.getValue("changeability"));
		}
		// coloca id do attributo na pilha
		this.elements.push(xmiAtt);
		// coloca o atributo relacionado com seu id no mapa
	}

	/**
	 * End UML:Attribute at UML:Class
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 2, tag = "UML:Class")
	@EndElement(tag = "UML:Attribute")
	public void endElementAttribute(ContextVariables contextVariables) {

		SAXEvent event = contextVariables.getLastEvent();

		XmiAttribute tempAttribute = (XmiAttribute) this.elements.peek();
		this.elements.pop();

		XmiClass tmpClass = (XmiClass) this.elements.peek();
		tmpClass.addAttribute(tempAttribute);


	}
	/**
	 * start element UML:DataType at UML:Attribute to get the xmi.idref to
	 * attributes type
	 *
	 * @param contextVariables
	 */
	@BeforeElement(elementDeep = 2, tag = "UML:Attribute")
	@StartElement(tag = "UML:DataType")
	public void startTypeDataAttribute(ContextVariables contextVariables) {
		SAXEvent event = contextVariables.getLastEvent();

		XmiAttribute tempAtt = (XmiAttribute) this.elements.peek();

		tempAtt.setIdDataType(event.getAtributesHolder().getValue("xmi.idref"));

	}
	@BeforeElement(elementDeep = 4, tag = "UML:Attribute")
	@StartElement(tag = "UML:MultiplicityRange")
	public void startElementMultiplicityRange(ContextVariables contextVariables) {
		AttributesHolder attHolder = contextVariables.getLastEvent().getAtributesHolder();

		XmiAttribute xmiAttribute = (XmiAttribute) this.elements.peek();
		System.out.println("			Start MultiplicityRange: Attribute " + xmiAttribute.getName()
				);
		{
			xmiAttribute.setLowerMultiplicity(Integer.parseInt(attHolder.getValue("lower")));
			xmiAttribute.setUpperMultiplicity(Integer.parseInt((attHolder.getValue("upper"))));
		}
		System.out.println("			End MultiplicityRange");

	}

	@BeforeElement(elementDeep = 2, tag="UML:Attribute")
	@StartElement(tag = "UML:Expression")
	public void startElementExpression(ContextVariables contextVariables)
	{
		AttributesHolder attHolder = contextVariables.getLastEvent().getAtributesHolder();

		XmiAttribute xmiAttribute = (XmiAttribute) this.elements.peek();

		xmiAttribute.setInitialValue(attHolder.getValue("body"));


	}




}